package com.app.twth.ui.adjust.retallytask;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.app.twth.BR;
import com.app.twth.app.AppRouter;
import com.app.twth.app.Constant;
import com.app.twth.base.BaseActivity;
import com.app.twth.databinding.ActivityRetallyTaskBinding;
import com.app.twth.ui.adjust.retally.ReTally;
import com.app.twth.ui.adjust.retally.ReTallyctivity;
import com.app.twth.ui.adjust.retallytask.submit.ReTallyTaskSubmitctivity;
import com.app.twth.ui.tally.lpn.TalliedSku;
import com.app.twth.utils.EditTextUtil;

/**
 * @describe :
 */
public class ReTallyTaskActivity extends BaseActivity<ActivityRetallyTaskBinding, ReTallyTaskViewModel> {
    private String TAG = getClass().getSimpleName();

    private ReTallyLPNAdapter adapter;

    @Override
    protected void initListener() {
        viewModel.reTallyLiveData.observe(this, data -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("data", data);
            AppRouter.showActivityForResult(mContext, ReTallyTaskSubmitctivity.class, bundle, Constant.ForResult2);
        });
        viewModel.reTallyTaskLiveData.observe(this, data -> {
            if (adapter != null) {
                adapter.setData(viewModel.reTallyTaskLiveData.getValue().getIbLpnList());
            }
        });

        viewModel.errorResultCode.observe(this, code -> {
            showHint((String) code);
        });
        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });
    }

    protected String setTitle() {
        return "Re-Tally(List)";
    }

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        Log.e(TAG, "ReTallyTaskActivity");
        Bundle bundle = getIntent().getBundleExtra(AppRouter.K_EXTRA);
        viewModel.reTallyTaskLiveData.setValue((ReTally) bundle.getSerializable("data"));

        adapter = new ReTallyLPNAdapter(this, viewModel.reTallyTaskLiveData.getValue().getIbLpnList(), position -> {
            TalliedSku talliedSku = viewModel.reTallyTaskLiveData.getValue().getIbLpnList().get(position);
            viewModel.reTallyTaskLiveData.getValue().setIbLpn(talliedSku.getIbLpn());
            Bundle bundle2 = new Bundle();
            bundle2.putSerializable("data", viewModel.reTallyTaskLiveData.getValue());
            AppRouter.showActivityForResult(mContext, ReTallyTaskSubmitctivity.class, bundle2, Constant.ForResult2);

//            viewModel.tallyReTally(talliedSku.getIbLpn());

        });
        binding.recyclerView.setHasFixedSize(true);
        binding.recyclerView.setItemAnimator(new DefaultItemAnimator());

        LinearLayoutManager layoutManager1 = new LinearLayoutManager(this);
        layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerView.setLayoutManager(layoutManager1);
        //添加装饰类
//        binding.recyclerview.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST));
        binding.recyclerView.setAdapter(adapter);

        binding.tvCode.requestFocus();
        binding.tvCode.setOnEditorActionListener(EnterListenter);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        bottomLayoutBinding.tvHint.setText("");
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
        }
    }

    private TextView.OnEditorActionListener EnterListenter = new TextView.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                barcodeStr = binding.tvCode.getText().toString();
                setInventory();
            }
            return false;
        }
    };

    @Override
    public void getScanninCode() {
        super.getScanninCode();
        binding.tvCode.setText(barcodeStr);
        setInventory();
    }

    private void setInventory() {
        showHint("");
        //如果移到末尾，那么index则传入 editText.getText().length()
        EditTextUtil.setSelection(binding.tvCode);
//        viewModel.tallyReTally(barcodeStr);
        for (TalliedSku talliedSku : viewModel.reTallyTaskLiveData.getValue().getIbLpnList()) {
            if (barcodeStr.equals(talliedSku.getIbLpn())) {
                viewModel.reTallyTaskLiveData.getValue().setIbLpn(talliedSku.getIbLpn());
                Bundle bundle2 = new Bundle();
                bundle2.putSerializable("data", viewModel.reTallyTaskLiveData.getValue());
                AppRouter.showActivityForResult(mContext, ReTallyctivity.class, bundle2, Constant.ForResult2);
                return;
            }
        }
        showHint("The LPN does not exist or has been completed");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.ForResult2 && resultCode == RESULT_OK) {
            Log.e(TAG, "onActivityResult");
            viewModel.tallyReTally();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
